package csci4448.android.workouttracker;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

public class GPSModel implements LocationListener {
	private LocationManager _locationManager;
	private double _currentLatitude;
	private double _currentLongitude;
	private boolean _hasLocation;
	private boolean _canListen;
	public GPSModel(Context context)
	{
		_canListen = true;
		_hasLocation = true;
		_locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
		_locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
	}
	public boolean hasLocation(){
		return _hasLocation;
	}
	public boolean canListen(){
		return _canListen;
	}
	public double getLatitude(){
		return _currentLatitude;
	}
	public double getLongitude(){
		return _currentLongitude;
	}
	public void onLocationChanged(Location arg0) {
		_hasLocation = true;
		_currentLatitude = arg0.getLatitude();
		_currentLongitude = arg0.getLongitude();
	}
	public void onProviderDisabled(String arg0) {
		_canListen = false;
	}
	public void onProviderEnabled(String arg0) {
		_canListen = true;
	}
	public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
		switch(arg1)
		{
			case android.location.LocationProvider.TEMPORARILY_UNAVAILABLE:
			case android.location.LocationProvider.OUT_OF_SERVICE:
				_canListen = false;
				_hasLocation = false;
				break;
			case android.location.LocationProvider.AVAILABLE:
				_canListen = true;
				break;
		}
	}
}
